#include "qremoveformdialog.h"
#include "ui_qremoveformdialog.h"

#include "../../libs/sharedLib/qprojectcore.h"
#include "../../libs/sharedLib/qpagemanager.h"
#include "../../libs/sharedLib/qpageinfo.h"

QRemoveFormDialog::QRemoveFormDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::QRemoveFormDialog)
{
    ui->setupUi(this);

    SHAREDLIB::QPageManager *p=SHAREDLIB::QProjectCore::getCore()->pageManager();

    int count=p->pageCount();

    SHAREDLIB::QPageInfo* info;
    QTreeWidgetItem *item;
    QTreeWidgetItem *par;
    for(int i=0;i<count;i++)
    {
        info=p->getPage(i);
        par=m_type_item.value(info->get_type());
        if(par==NULL)
        {
            par=new QTreeWidgetItem(ui->treeWidget);
            par->setText(0,info->get_type());
            par->setExpanded(true);
            m_type_item.insert(info->get_type(),par);
        }
        item=new QTreeWidgetItem(par);
        item->setText(0,info->get_name());
        item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable);
        item->setCheckState(0,Qt::Unchecked);
        m_items.insert(item,info);
    }
}

QRemoveFormDialog::~QRemoveFormDialog()
{
    delete ui;
}

void QRemoveFormDialog::on_okBtn_clicked()
{
    QList<SHAREDLIB::QPageInfo*> list;
    QMapIterator<QTreeWidgetItem*,SHAREDLIB::QPageInfo*> it(m_items);
    while(it.hasNext())
    {
        it.next();
        if(it.key()->checkState(0)==Qt::Checked)
        {
            list.append(it.value());
        }
    }
    while(list.size()>0)
    {
        SHAREDLIB::QProjectCore::getCore()->pageManager()->removePage(list.first());
        list.removeFirst();
    }
    close();
}

void QRemoveFormDialog::on_cancelBtn_clicked()
{
    close();
}
